/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.synchronization.remote.ws.converter;

import java.math.BigDecimal;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import systole.domain.analysis.Analysis;
import systole.domain.analysis.results.AnalysisResult;
import systole.domain.analysis.results.Parameter;
import systole.domain.clinicalInformation.ClinicalInformation;
import systole.domain.clinicalInformation.Family;
import systole.domain.clinicalInformation.FamilyPatientBackground;
import systole.domain.clinicalInformation.HabitPatient;
import systole.domain.clinicalInformation.Medicine;
import systole.domain.clinicalInformation.MedicinePatient;
import systole.domain.clinicalInformation.Pathology;
import systole.domain.clinicalInformation.PathologyPatient;
import systole.domain.clinicalInformation.Sport;
import systole.domain.clinicalInformation.SportPatient;
import systole.domain.clinicalInformation.Surgery;
import systole.domain.clinicalInformation.SurgeryPatient;
import systole.domain.persons.Medic;
import systole.domain.persons.Patient;
import systole.domain.persons.identityCard.IdentityCard;
import systole.domain.persons.identityCard.IdentityCardType;
import systole.domain.persons.profession.Profession;
import systole.domain.report.Report;
import systole.domain.signals.FinalSignal;
import systole.domain.signals.RawSignal;
import systole.domain.signals.Segment;
import systole.domain.signals.SelectedSegment;
import systole.domain.signals.SignalFrequency;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.synchronization.remote.entities.ClinicalInfoRemote;
import systole.synchronization.remote.entities.FamilyRemote;
import systole.synchronization.remote.entities.IdentityCardTypeRemote;
import systole.synchronization.remote.entities.MedicRemote;
import systole.synchronization.remote.entities.MedicineRemote;
import systole.synchronization.remote.entities.PathologyRemote;
import systole.synchronization.remote.entities.PatientRemote;
import systole.synchronization.remote.entities.ProfessionRemote;
import systole.synchronization.remote.entities.SignalFrequencyRemote;
import systole.synchronization.remote.entities.SportRemote;
import systole.synchronization.remote.entities.SurgeryRemote;
import systole.synchronization.remote.ws.AnalysisResultWs;
import systole.synchronization.remote.ws.AnalysisWs;
import systole.synchronization.remote.ws.ClinicalInfoWs;
import systole.synchronization.remote.ws.FamilyWs;
import systole.synchronization.remote.ws.FinalSignalWs;
import systole.synchronization.remote.ws.IdentityCardTypeWs;
import systole.synchronization.remote.ws.IdentityCardWs;
import systole.synchronization.remote.ws.MedicWs;
import systole.synchronization.remote.ws.MedicineWs;
import systole.synchronization.remote.ws.ParameterCoordWs;
import systole.synchronization.remote.ws.PathologyWs;
import systole.synchronization.remote.ws.PatientFamilyWs;
import systole.synchronization.remote.ws.PatientHabitsWs;
import systole.synchronization.remote.ws.PatientMedicineWs;
import systole.synchronization.remote.ws.PatientPathologyWs;
import systole.synchronization.remote.ws.PatientSportWs;
import systole.synchronization.remote.ws.PatientSurgeryWs;
import systole.synchronization.remote.ws.PatientWs;
import systole.synchronization.remote.ws.ProfessionWs;
import systole.synchronization.remote.ws.RawSignalWs;
import systole.synchronization.remote.ws.ReportWs;
import systole.synchronization.remote.ws.SegmentWs;
import systole.synchronization.remote.ws.SelectedSegmentWs;
import systole.synchronization.remote.ws.SignalFrequencyWs;
import systole.synchronization.remote.ws.SportWs;
import systole.synchronization.remote.ws.SurgeryWs;

/**
 *
 * @author jmj
 */
public class ConverterToRemoteEntity {

    /**
     *
     * @param patient
     * @return
     * @throws ExceptionDAO
     */
    public PatientWs generateRemotePatient(Patient patient) throws ExceptionDAO {

        if (patient == null) {
            return null;
        }
        PatientWs patientWs = new PatientWs();
        patientWs.setName(patient.getName());
        patientWs.setSurname(patient.getSurname());
        patientWs.setAddress(patient.getAddress());
        patientWs.setCellphone(patient.getCellphone());
        patientWs.setCity(patient.getCity());
        patientWs.setEmail(patient.getEmail());
        patientWs.setTelephone(patient.getTelephone());
        patientWs.setSex(("M".equals(patient.getSex()) || "m".equals(patient.getSex())));

        patientWs.setBirthdate(this.dateToXMLGregorianCalendar(patient.getBirthdate()));

        patientWs.setIdentityCard(this.convertToRemoteIdentityCard(patient.getIdentityCard()));
        patientWs.setHabits(this.convertToRemoteHabits(patient.getHabitPatient()));
        patientWs.setProfessionId(this.convertToRemoteProfession(patient.getProfession()));

        if (patient.getSportsPatient() != null) {
            Iterator<SportPatient> sports = patient.getSportsPatient().iterator();
            while (sports.hasNext()) {
                PatientSportWs patientSportWs = this.convertToRemoteSportPatient(sports.next());
                if (patientSportWs != null) {
                    patientWs.getSports().add(patientSportWs);
                }
            }
        }

        if (patient.getMedicinesPatient() != null) {
            Iterator<MedicinePatient> medicines = patient.getMedicinesPatient().iterator();
            while (medicines.hasNext()) {
                PatientMedicineWs patientMedicineWs = this.convertToRemoteMedicinePatient(medicines.next());
                if (patientMedicineWs != null) {
                    patientWs.getMedicines().add(patientMedicineWs);
                }
            }
        }

        if (patient.getFamilyPatientBackgrounds() != null) {
            Iterator<FamilyPatientBackground> backgrounds = patient.getFamilyPatientBackgrounds().iterator();
            while (backgrounds.hasNext()) {
                PatientFamilyWs patientFamilyWs = this.convertToRemoteFamilyPatient(backgrounds.next());
                if (patientFamilyWs != null) {
                    patientWs.getFamilyBackground().add(patientFamilyWs);
                }
            }
        }

        if (patient.getPathologiesPatient() != null) {
            Iterator<PathologyPatient> patholiges = patient.getPathologiesPatient().iterator();
            while (patholiges.hasNext()) {
                PatientPathologyWs pathologyWs = this.convertToRemotePathologyPatient(patholiges.next());
                if (pathologyWs != null) {
                    patientWs.getPathologies().add(pathologyWs);
                }
            }
        }

        if (patient.getSurgeriesPatient() != null) {
            Iterator<SurgeryPatient> surgeries = patient.getSurgeriesPatient().iterator();
            while (surgeries.hasNext()) {
                PatientSurgeryWs patientSurgeryWs = this.convertToRemoteSurgeryPatient(surgeries.next());
                if (patientSurgeryWs != null) {
                    patientWs.getSurgeries().add(patientSurgeryWs);
                }
            }
        }
        return patientWs;
    }

    /**
     *
     * @param identityCard
     * @return
     * @throws ExceptionDAO
     */
    private IdentityCardWs convertToRemoteIdentityCard(IdentityCard identityCard) throws ExceptionDAO {
        if (identityCard == null) {
            return null;
        }
        IdentityCardTypeRemote cardTypeRemote = FacadeDB.getInstance().getIdentityCardTypeSyncBroker().getIdentityCardTypeRemoteFromCardType(identityCard.getCardType());

        if (cardTypeRemote == null) {
            return null;
        }

        IdentityCardWs cardWs = new IdentityCardWs();
        cardWs.setTypeId(cardTypeRemote.getRemoteId());
        cardWs.setNumber(identityCard.getNumber() != null ? identityCard.getNumber() : 0);
        return cardWs;
    }

    /**
     * 
     * @param profession
     * @return
     * @throws ExceptionDAO
     */
    private Integer convertToRemoteProfession(Profession profession) throws ExceptionDAO {
        if (profession == null) {
            return null;
        }
        ProfessionRemote professionRemote = FacadeDB.getInstance().getProfessionSyncBroker().getProfessionRemoteByProfession(profession);

        return professionRemote != null ? professionRemote.getRemoteId() : null;

    }

    /**
     *
     * @param habitPatient
     * @return
     */
    private PatientHabitsWs convertToRemoteHabits(HabitPatient habitPatient) {
        if (habitPatient == null) {
            return null;
        }
        PatientHabitsWs habitsWs = new PatientHabitsWs();
        habitsWs.setAgeAtStart(habitPatient.getYearsOldAtStart() != null ? habitPatient.getYearsOldAtStart() : 0);
        habitsWs.setAgeAtEnd(habitPatient.getYearsOldAtFinish() != null ? habitPatient.getYearsOldAtFinish() : 0);
        habitsWs.setAngryLevel(habitPatient.getAngryLevel() != null ? habitPatient.getAngryLevel() : 0);
        habitsWs.setStressLevel(habitPatient.getStressLevel() != null ? habitPatient.getStressLevel() : 0);
        habitsWs.setAutomedication(habitPatient.getAutomedication() != null ? habitPatient.getAutomedication() : false);
        habitsWs.setConsummingAlcohol(habitPatient.getConsummingAlcohol() != null ? habitPatient.getConsummingAlcohol() : false);
        habitsWs.setWorkPlace(habitPatient.getWorkPlace());
        habitsWs.setSmoked(habitPatient.getSmoked() != null ? habitPatient.getSmoked() : false);
        habitsWs.setSmoking(habitPatient.getSmoking() != null ? habitPatient.getSmoking() : false);
        habitsWs.setCigarettesPerDay(habitPatient.getCigarettesPerDay() != null ? habitPatient.getCigarettesPerDay() : 0);
        habitsWs.setBlocksWalkedPerDay(habitPatient.getBlocksWalkedPerDay() != null ? habitPatient.getBlocksWalkedPerDay() : 0);
        habitsWs.setSedentary(habitPatient.getSedentary() != null ? habitPatient.getSedentary() : false);
        return habitsWs;
    }

    /**
     *
     * @param sportPatient
     * @return
     */
    private PatientSportWs convertToRemoteSportPatient(SportPatient sportPatient) throws ExceptionDAO {
        if (sportPatient == null) {
            return null;
        }

        SportRemote sportRemote = FacadeDB.getInstance().getSportSyncBroker().getSportRemoteFromSport(sportPatient.getSport());
        if (sportRemote == null) {
            return null;
        }
        PatientSportWs patientSportWs = new PatientSportWs();
        patientSportWs.setAgeAtStart(sportPatient.getAgeAtStartAsInteger() > 0 ? sportPatient.getPatient().getAge() - sportPatient.getAgeAtStartAsInteger() : 0);
        patientSportWs.setHoursXWeek(sportPatient.getHoursXWeek() != null ? sportPatient.getHoursXWeek() : 0);
        patientSportWs.setSportId(sportRemote.getRemoteId());
        return patientSportWs;
    }

    /**
     *
     * @param medicinePatient
     * @return
     * @throws ExceptionDAO
     */
    private PatientMedicineWs convertToRemoteMedicinePatient(MedicinePatient medicinePatient) throws ExceptionDAO {
        if (medicinePatient == null) {
            return null;
        }

        MedicineRemote medicineRemote = FacadeDB.getInstance().getMedicineSyncBroker().getMedicineRemoteByMedicine(medicinePatient.getMedicine());
        if (medicineRemote == null) {
            return null;
        }
        PatientMedicineWs patientMedicineWs = new PatientMedicineWs();
        patientMedicineWs.setAgeAtStart(medicinePatient.getAgeAtStartAsInteger() > 0 ? medicinePatient.getPatient().getAge() - medicinePatient.getAgeAtStartAsInteger() : 0);
        patientMedicineWs.setDosage(medicinePatient.getDosage());
        patientMedicineWs.setMedicineId(medicineRemote.getRemoteId());
        return patientMedicineWs;
    }

    /**
     *
     * @param background
     * @return
     * @throws ExceptionDAO
     */
    private PatientFamilyWs convertToRemoteFamilyPatient(FamilyPatientBackground background) throws ExceptionDAO {
        if (background == null) {
            return null;
        }

        FamilyRemote familyRemote = FacadeDB.getInstance().getFamilySyncBroker().getFamilyRemoteByFamily(background.getFamily());
        if (familyRemote == null) {
            return null;
        }
        PatientFamilyWs familyWs = new PatientFamilyWs();
        familyWs.setFamilyId(familyRemote.getRemoteId());
        familyWs.setBackgroundCardiac(background.getBackgroundCardiac() != null ? background.getBackgroundCardiac() : false);
        familyWs.setBackgroundCholesterol(background.getBackgroundCholesterol() != null ? background.getBackgroundCholesterol() : false);
        familyWs.setBackgroundDiabetic(background.getBackgroundDiabetic() != null ? background.getBackgroundDiabetic() : false);
        familyWs.setBackgroundHypertension(background.getBackgroundHypertension() != null ? background.getBackgroundHypertension() : false);
        return familyWs;
    }

    /**
     * 
     * @param pathologyPatient
     * @return
     * @throws ExceptionDAO
     */
    private PatientPathologyWs convertToRemotePathologyPatient(PathologyPatient pathologyPatient) throws ExceptionDAO {
        if (pathologyPatient == null) {
            return null;
        }

        PathologyRemote pathologyRemote = FacadeDB.getInstance().getPathologySyncBroker().getPathologyRemoteByPathology(pathologyPatient.getPathology());
        if (pathologyRemote == null) {
            return null;
        }
        PatientPathologyWs patientPathologyWs = new PatientPathologyWs();
        patientPathologyWs.setPathologyId(pathologyRemote.getRemoteId());
        patientPathologyWs.setAgeAtStart(pathologyPatient.getAgeAtStartAsInteger() > 0 ? pathologyPatient.getPatient().getAge() - pathologyPatient.getAgeAtStartAsInteger() : 0);
        patientPathologyWs.setCurrently(pathologyPatient.getCurrently() != null ? pathologyPatient.getCurrently() : false);
        patientPathologyWs.setInherited(pathologyPatient.getInherited() != null ? pathologyPatient.getInherited() : false);
        return patientPathologyWs;
    }

    /**
     *
     * @param surgeryPatient
     * @return
     * @throws ExceptionDAO
     */
    private PatientSurgeryWs convertToRemoteSurgeryPatient(SurgeryPatient surgeryPatient) throws ExceptionDAO {
        if (surgeryPatient == null) {
            return null;
        }
        SurgeryRemote surgeryRemote = FacadeDB.getInstance().getSurgerySyncBroker().getSurgeryRemoteBySurgery(surgeryPatient.getSurgery());
        if (surgeryRemote == null) {
            return null;
        }
        PatientSurgeryWs patientSurgeryWs = new PatientSurgeryWs();
        patientSurgeryWs.setSurgeryId(surgeryRemote.getRemoteId());
        patientSurgeryWs.setAgeAtSurgery(surgeryPatient.getAgeAtSurgery() != null ? surgeryPatient.getAgeAtSurgery() : 0);
        return patientSurgeryWs;
    }

    /**
     *
     * @param medic
     * @return
     * @throws ExceptionDAO 
     */
    public MedicWs generateRemoteMedic(Medic medic) throws ExceptionDAO {
        if (medic == null) {
            return null;
        }
        MedicWs medicWs = new MedicWs();
        medicWs.setName(medic.getName());
        medicWs.setSurname(medic.getSurname());
        medicWs.setCellphone(medic.getCellphone());
        medicWs.setTelephone(medic.getTelephone());
        medicWs.setAddress(medic.getAddress());
        medicWs.setCity(medic.getCity());
        medicWs.setEmail(medic.getEmail());
        medicWs.setIdentityCard(this.convertToRemoteIdentityCard(medic.getIdentityCard()));
        medicWs.setSex(medic.getSex() == null ? null : ("M".equals(medic.getSex()) || "m".equals(medic.getSex())));
        medicWs.setBirthdate(this.dateToXMLGregorianCalendar(medic.getBirthdate()));

        return medicWs;
    }

    /**
     * 
     * @param medicine
     * @return
     */
    public MedicineWs generateRemoteMedicine(Medicine medicine) {
        if (medicine == null) {
            return null;
        }
        MedicineWs medicineWs = new MedicineWs();
        medicineWs.setName(medicine.getName());
        medicineWs.setDescription(medicine.getDescription() != null ? medicine.getDescription() : "");
        return medicineWs;
    }

    /**
     * 
     * @param clinicalInformation
     * @return
     * @throws ExceptionDAO
     */
    public ClinicalInfoWs generateRemoteClinicalInformation(ClinicalInformation clinicalInformation) throws ExceptionDAO {
        if (clinicalInformation == null) {
            return null;
        }
        PatientRemote patientRemote = FacadeDB.getInstance().getPatientSyncBroker().getPatientRemoteByPatient(clinicalInformation.getPatient());
        if (patientRemote == null) {
            return null;
        }

        ClinicalInfoWs clinicalInfoWs = new ClinicalInfoWs();
        clinicalInfoWs.setPatientId(patientRemote.getRemoteId());
        clinicalInfoWs.setAge(clinicalInformation.getYears());
        clinicalInfoWs.setDate(this.dateToXMLGregorianCalendar(clinicalInformation.getInformationDate()));
        clinicalInfoWs.setWeight(clinicalInformation.getWeight());
        clinicalInfoWs.setHeight(clinicalInformation.getHeight());
        clinicalInfoWs.setHdl(clinicalInformation.getHdl());
        clinicalInfoWs.setLdl(clinicalInformation.getLdl());
        clinicalInfoWs.setGlucemic(clinicalInformation.getGlucemic());
        clinicalInfoWs.setTotalCholesterol(clinicalInformation.getTotalCholesterol());
        clinicalInfoWs.setSystolicPressure(clinicalInformation.getSystolicPressure());
        clinicalInfoWs.setDiastolicPressure(clinicalInformation.getDiastolicPressure());
        clinicalInfoWs.setTriglycerides(clinicalInformation.getTriglycerides());
        return clinicalInfoWs;
    }

    /**
     * 
     * @param pathology
     * @return
     */
    public PathologyWs generateRemotePathology(Pathology pathology) {
        if (pathology == null) {
            return null;
        }
        PathologyWs pathologyWs = new PathologyWs();
        pathologyWs.setDescription(pathology.getDescription());
        pathologyWs.setInitials(pathology.getInitials());
        pathologyWs.setName(pathology.getName());
        return pathologyWs;
    }

    /**
     * 
     * @param sport
     * @return
     */
    public SportWs generateRemoteSport(Sport sport) {
        if (sport == null) {
            return null;
        }
        SportWs sportWs = new SportWs();
        sportWs.setDescription(sport.getDescription());
        sportWs.setName(sport.getName());
        return sportWs;
    }

    /**
     * 
     * @param frequency
     * @return
     */
    public SignalFrequencyWs generateRemoteSignalFrequency(SignalFrequency frequency) {
        if (frequency == null) {
            return null;
        }
        SignalFrequencyWs frequencyWs = new SignalFrequencyWs();
        frequencyWs.setFrequency(frequency.getFrequency() != null ? frequency.getFrequency().floatValue() : 1f);
        frequencyWs.setName(frequency.getDescription());
        return frequencyWs;

    }

    /**
     * 
     * @param profession
     * @return
     */
    public ProfessionWs generateRemoteProfessionRemote(Profession profession) {
        if (profession == null) {
            return null;
        }
        ProfessionWs professionWs = new ProfessionWs();
        professionWs.setName(profession.getName());
        professionWs.setDescription(profession.getDescription());
        return professionWs;
    }

    /**
     *
     * @param surgery
     * @return
     */
    public SurgeryWs generateRemoteSurgery(Surgery surgery) {
        if (surgery == null) {
            return null;
        }
        SurgeryWs surgeryWs = new SurgeryWs();
        surgeryWs.setDescription(surgery.getDescription());
        surgeryWs.setName(surgery.getName());
        return surgeryWs;
    }

    /**
     * 
     * @param cardType
     * @return
     */
    public IdentityCardTypeWs generateRemoteIdentityCard(IdentityCardType cardType) {
        if (cardType == null) {
            return null;
        }
        IdentityCardTypeWs cardTypeWs = new IdentityCardTypeWs();
        cardTypeWs.setName(cardType.getDescription());
        return cardTypeWs;
    }

    /**
     * 
     * @param family
     * @return
     */
    public FamilyWs generateRemoteFamily(Family family) {
        if (family == null) {
            return null;
        }
        FamilyWs familyWs = new FamilyWs();
        familyWs.setName(family.getName());
        familyWs.setSex("M".equals(family.getSex()) || "m".equals(family.getSex()));
        return familyWs;
    }

    /**
     *
     * @param analysis
     * @return
     * @throws ExceptionDAO
     */
    public AnalysisWs generateRemoteAnalysis(Analysis analysis) throws ExceptionDAO {
        if (analysis == null) {
            return null;
        }

        AnalysisWs analysisWs = new AnalysisWs();

        PatientRemote patientRemote = FacadeDB.getInstance().getPatientSyncBroker().getPatientRemoteByPatient(analysis.getPatient());

        if (patientRemote == null) {
            return null;
        }
        analysisWs.setPatientId(patientRemote.getRemoteId());

        if (analysis.getMedic() != null) {
            MedicRemote medicRemote = FacadeDB.getInstance().getMedicSyncBroker().getMedicRemoteByMedic(analysis.getMedic());
            analysisWs.setMedicId(medicRemote.getRemoteId());
        }

        SignalFrequencyRemote frequencyRemote = FacadeDB.getInstance().getSignalFrequencySyncBroker().getFrequencyRemoteBySignalFrequency(analysis.getSignalFrequency());

        if (frequencyRemote == null) {
            return null;
        }

        analysisWs.setSignalFrequencyId(frequencyRemote.getRemoteId());

        ClinicalInfoRemote clinicalInfoRemote = FacadeDB.getInstance().getClinicalInfoSyncBroker().getClinicalInfoRemoteByClinicalInfo(analysis.getClinicalInformation());

        if (clinicalInfoRemote == null) {
            return null;
        }
        analysisWs.setClinicalInfoId(clinicalInfoRemote.getRemoteId());

        analysisWs.setDate(this.dateToXMLGregorianCalendar(analysis.getAnalysisDay()));
        analysisWs.setRawSignal(this.convertToRemoteRawSignal(analysis.getRawSignal(), analysis));
        analysisWs.setReport(this.convertToRemoteReport(analysis.getReport()));
        analysisWs.setResult(this.convetToRemoteAnalysisResult(analysis.getAnalysisResult()));
        analysisWs.setFinalSignal(this.convertToRemoteFinalSignal(analysis.getFinalSegment()));
        Iterator<SelectedSegment> segments = analysis.getSelectedSegments().iterator();
        while (segments.hasNext()) {
            SelectedSegmentWs selectedSegmentWs = this.convetToRemoteSelectedSegments(segments.next());
            if (selectedSegmentWs != null) {
                analysisWs.getSelectedSegments().add(selectedSegmentWs);
            }
        }
        return analysisWs;
    }

    /**
     *
     * @param finalSignal
     * @return
     */
    private FinalSignalWs convertToRemoteFinalSignal(FinalSignal finalSignal) {
        if (finalSignal == null) {
            return null;
        }

        FinalSignalWs finalSignalWs = new FinalSignalWs();
        finalSignalWs.setFinalSegment(this.convertToRemoteSegment(finalSignal.getFinalSegment()));
        finalSignalWs.setFirstDerivatite(this.convertToRemoteSegment(finalSignal.getFirstDerivatite()));
        finalSignalWs.setSecondDerivative(this.convertToRemoteSegment(finalSignal.getSecondDerivative()));
        finalSignalWs.setThirdDerivative(this.convertToRemoteSegment(finalSignal.getThirdDerivative()));
        finalSignalWs.setFourthDerivative(this.convertToRemoteSegment(finalSignal.getFourthDerivative()));
        finalSignalWs.setExponential(this.convertToRemoteSegment(finalSignal.getExponential()));
        return finalSignalWs;
    }

    /**
     *
     * @param selectedSegment
     * @return
     */
    private SelectedSegmentWs convetToRemoteSelectedSegments(SelectedSegment selectedSegment) {
        if (selectedSegment == null) {
            return null;
        }

        SegmentWs segmentWs = this.convertToRemoteSegment(selectedSegment.getSegment());
        SelectedSegmentWs selectedSegmentWs = new SelectedSegmentWs();
        selectedSegmentWs.setSegment(segmentWs);
        selectedSegmentWs.setNumber(selectedSegment.getNumber());
        return selectedSegmentWs;
    }

    /**
     *
     * @param analysisResult
     * @return
     */
    private AnalysisResultWs convetToRemoteAnalysisResult(AnalysisResult analysisResult) {
        if (analysisResult == null) {
            return null;
        }
        AnalysisResultWs analysisResultWs = new AnalysisResultWs();
        analysisResult.setHeartRate(analysisResult.getHeartRate());
        analysisResultWs.setAod(this.convetToRemoteCoor(analysisResult.getAod()));
        analysisResultWs.setAos(this.convetToRemoteCoor(analysisResult.getAos()));
        analysisResultWs.setIar(this.convetToRemoteCoor(analysisResult.getIar()));
        analysisResultWs.setSystolicDelay(this.convetToRemoteCoor(analysisResult.getT()));
        return analysisResultWs;
    }

    /**
     *
     * @param parameter
     * @return
     */
    private ParameterCoordWs convetToRemoteCoor(Parameter parameter) {
        if (parameter == null) {
            return null;
        }
        ParameterCoordWs parameterCoordWs = new ParameterCoordWs();
        parameterCoordWs.setX1(parameter.getInitPos().getxCoord().floatValue());
        parameterCoordWs.setY1(parameter.getInitPos().getyCoord().floatValue());
        parameterCoordWs.setX2(parameter.getEndPos().getxCoord().floatValue());
        parameterCoordWs.setY2(parameter.getEndPos().getyCoord().floatValue());
        return parameterCoordWs;

    }

    /**
     * 
     * @param report
     * @return
     */
    private ReportWs convertToRemoteReport(Report report) {
        if (report == null) {
            return null;
        }
        ReportWs reportWs = new ReportWs();
        reportWs.setBackgroundFamily(report.getBackgroundFamily());
        reportWs.setBackgroundMedicines(report.getBackgroundMedicines());
        reportWs.setBackgroundPathologies(report.getBackgroundPathologies());
        reportWs.setBackgroundSurgeries(report.getBackgroundSurgeries());
        reportWs.setCommentAod(report.getCommentAOD());
        reportWs.setCommentAos(report.getCommentAOS());
        reportWs.setCommentIar(report.getCommentIAR());
        reportWs.setComments(report.getComments());
        reportWs.setConclusion(report.getConclusion());
        reportWs.setHabitsSummary(report.getHabitsSummary());
        return reportWs;
    }

    /**
     *
     * @param rawSignal
     * @param analysis
     * @return
     */
    private RawSignalWs convertToRemoteRawSignal(RawSignal rawSignal, Analysis analysis) {
        if (rawSignal == null) {
            return null;
        }
        RawSignalWs rawSignalWs = new RawSignalWs();
        rawSignalWs.setFileName(analysis.getFileName());
        rawSignalWs.setSignal(this.convertToRemoteSegment(rawSignal.getSegment()));
        return rawSignalWs;
    }

    /**
     *
     * @param segment
     * @return
     */
    private SegmentWs convertToRemoteSegment(Segment segment) {
        if (segment == null) {
            return null;
        }
        SegmentWs segmentWs = new SegmentWs();
        Iterator<BigDecimal> points = segment.iterator();
        while (points.hasNext()) {
            segmentWs.getPoint().add(points.next().floatValue());
        }
        return segmentWs;
    }

    /**    
     * 
     * @param date
     * @return
     */
    private XMLGregorianCalendar dateToXMLGregorianCalendar(Date date) {
        XMLGregorianCalendar xmlgc = null;
        GregorianCalendar cal = new GregorianCalendar();
        if (date != null) {
            cal.setTime(date);
        }
        try {
            xmlgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);

        } catch (DatatypeConfigurationException e) {
        }

        return xmlgc;
    }
}
